package com.example.demo.ecigarette;

import cn.hutool.core.util.HexUtil;

/**
 * <pre>
 *  描述:
 * </pre>
 *
 * @author godelgnis(wujiaer)
 * @version 1.0.0
 * @date 2021/12/16 17:31
 */
public class Ecigarette {
    public static void main(String[] args) {
        printPackage("00F66F0100010001000100010001000163006300000063100000000000");
        printPackage("00F66F0100010001000400040001000140004000000063100000000000");
        printPackage("00F66F0100010001000200020001000150005000000063100000000000");
    }

    static void printPackage(String hexBody) {
        String head = "7F01";
        String imei = "0862211234512345";
        String cmdId = "01";
        String bodyLength = "001E";
        String body = hexBody;
        String check1 = getCheckSum(HexUtil.decodeHex(body));
        String result = head + imei + cmdId + bodyLength + body + check1;
        String check2 = getCheckSum(HexUtil.decodeHex(result));
        System.out.println(result + check2);
    }

    public static String getCheckSum(byte[] checkArray) {
        int localCheck = 0;
        System.out.println("checkArray length: " + checkArray.length);
        for (int i = 0; i < checkArray.length; i ++) {
            localCheck = localCheck + Byte.toUnsignedInt(checkArray[i]);
        }
        System.out.println("localCheck: " + localCheck);
        return HexUtil.encodeHexStr(new byte[]{(byte)(localCheck & 0xFF)}).toUpperCase();
    }
}
